/*
 *  Copyright 2017 Google Inc. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.google.blockly.ohos.ui;

import com.google.blockly.model.BlocklyCategory;
import com.google.blockly.utils.ColorUtils;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * View wrapper for toolbox components.
 */
public class CategoryView extends DependentLayout {
    public static final int DEFAULT_CATEGORIES_BACKGROUND_ALPHA = 0xFF;
    public static final int DEFAULT_CATEGORIES_BACKGROUND_COLOR = Color.LTGRAY.getValue();
    protected static final float BLOCKS_BACKGROUND_LIGHTNESS = 0.75f;

    protected CategoryTabs mCategoryTabs;

    protected BlocklyCategory mRootCategory;
    protected BlocklyCategory mCurrentCategory;

    // The current state of the toolbox being closeable or not.
    private boolean mCloseable = true;

    public CategoryView(Context context, AttrSet attrs) {
        super(context, attrs);
    }


    public void setCategoryTabs(CategoryTabs categoryTabs) {
        mCategoryTabs = categoryTabs;
    }

    public void setCallback(CategorySelectorUI.Callback callback) {
        if (mCategoryTabs != null) {
            mCategoryTabs.setCallback(callback);
        }
    }

    public void reset() {
        mCategoryTabs.setCategories(new ArrayList<BlocklyCategory>(0));
    }

    /**
     * Set the root category for the toolbox. This top level category must contain
     * a list of subcategories or be null. If it has subcategories, it will
     * render each subcategory with its own tab.
     *
     * @param topLevelCategory The top-level category in the toolbox.
     */
    public void setContents(final BlocklyCategory topLevelCategory) {
        mRootCategory = topLevelCategory;
        if (topLevelCategory == null) {
            reset();
            return;
        }
        List<BlocklyCategory> subcats = topLevelCategory.getSubcategories();

        if (subcats.isEmpty()) {
            throw new IllegalArgumentException("Contents must be a set of subcategories.");
        }

        // If we have subcategories, use the closeable preference.
        mCategoryTabs.setCategories(subcats);
        mCategoryTabs.setVisibility(VISIBLE);
        setCurrentCategory(mCloseable ? null : subcats.get(0));
        mCategoryTabs.setTapSelectedDeselects(mCloseable);

        updateCategoryColors(topLevelCategory);
    }

    public void setCurrentCategory(BlocklyCategory category) {
        if (category == mCurrentCategory) {
            return;
        }
        mCurrentCategory = category;
        mCategoryTabs.setSelectedCategory(category);
        updateCategoryColors(category);
    }

    public BlocklyCategory getCurrentCategory() {
        return mCurrentCategory;
    }

    public boolean isCloseable() {
        return mCloseable;
    }

    public void setScrollOrientation(int orientation) {
        if (mCategoryTabs != null) {
            mCategoryTabs.setOrientation(orientation);
        }
    }

    protected void updateCategoryColors(BlocklyCategory curCategory) {
        Integer maybeColor = curCategory == null ? null : curCategory.getColor();
        int bgColor = DEFAULT_CATEGORIES_BACKGROUND_COLOR;
        if (maybeColor != null) {
            bgColor = getBackgroundColor(maybeColor);
        }

        RgbColor rgbColor = RgbColor.fromArgbInt(bgColor);
        rgbColor.setAlpha(mCloseable ? DEFAULT_CATEGORIES_BACKGROUND_ALPHA : ColorUtils.ALPHA_OPAQUE);
        rgbColor.setRed(rgbColor.getRed());
        rgbColor.setGreen(rgbColor.getGreen());
        rgbColor.setBlue(rgbColor.getBlue());

        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(rgbColor);
        this.setBackground(shapeElement);
    }

    protected int getBackgroundColor(int categoryColor) {
        return ColorUtils.blendRGB(categoryColor, Color.WHITE.getValue(), BLOCKS_BACKGROUND_LIGHTNESS);
    }
}
